﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Media;
using System.Threading;

namespace Elevator
{
    public partial class Form1 : Form
    {
        enum direction
        {
            Up, Down            
        }

        direction which = new direction();
        enum floor
        {
            first,second,third           
        }

        floor goingTo = new floor();
        int liftSpeed ;
        Font myFont = new Font("Arial", 16);
        Bitmap needle;
        int myAngle=0;
        int myRatioUp = 0;
        int myRatioDown = 0;
        Thread motor = null;
        public delegate void driveLift();//delegate to help motor thread
        bool pauseThread = false;// checks if motor thread stopped

        public Form1()
        {
            InitializeComponent();
            bttnDown3.Enabled = false;
            bttnUp1.Enabled = false;
            bttnUp1to3.Enabled = false;
            bttnDown3to1.Enabled = false;
            label4.Text = ("Lift speed interval = 1ms");
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            goingTo = floor.second;
            drawNeedle();
            rotateNeedle();
           
        }

        public void drawNeedle()
        {
            needle = new Bitmap(5,40);
            Graphics g = Graphics.FromImage(needle);
            g.FillRectangle(Brushes.Red, 0, 0, 5, 40);
            g.FillRectangle(Brushes.Blue, 0, 0, 5, 5);
            

        }

        private void bttnDown3_Click(object sender, EventArgs e)//third floor
        {
            which = direction.Down;
            goingTo = floor.second;
            bttnDown3.Enabled = false;
            bttnDown3to1.Enabled = false;
            bttnUp2.Enabled = true;
            bttnDown2.Enabled = true;
            pauseThread = false;
              motor = new Thread(new ThreadStart(runThread));
             motor.Start();
            
            
        }
        private void bttnDown3to1_Click(object sender, EventArgs e)
        {
            which = direction.Down;
            goingTo = floor.first;
            bttnDown3.Enabled = false;
            bttnDown3to1.Enabled = false;
            bttnUp1to3.Enabled = true;
            bttnUp1.Enabled = true;
            pauseThread = false;
            motor = new Thread(new ThreadStart(runThread));
            motor.Start();
        }
     

        private void bttnDown2_Click(object sender, EventArgs e)//second floor
        {
            which = direction.Down;
            goingTo = floor.first;
            bttnUp2.Enabled = false;
            bttnDown2.Enabled = false;
            bttnUp1.Enabled = true;
            bttnUp1to3.Enabled = true;
            pauseThread = false;
            motor = new Thread(new ThreadStart(runThread));
            motor.Start();
         
        }

        private void bttnUp2_Click(object sender, EventArgs e)//second floor
        {
            which = direction.Up;
            goingTo = floor.third;
            bttnUp2.Enabled = false;
            bttnDown2.Enabled = false;
            bttnDown3.Enabled = true;
            bttnDown3to1.Enabled = true;
            pauseThread = false;         
            motor = new Thread(new ThreadStart(runThread));
            motor.Start();
        }

        private void bttnUp1_Click(object sender, EventArgs e)//first floor
        {
            which = direction.Up;
            goingTo = floor.second;
            bttnUp1.Enabled = false;
            bttnUp1to3.Enabled = false;
            bttnUp2.Enabled = true;
            bttnDown2.Enabled = true;
            pauseThread = false;            
            motor = new Thread(new ThreadStart(runThread));
            motor.Start();
         
        }
        private void bttnUp1to3_Click(object sender, EventArgs e)
        {
            which = direction.Up;
            goingTo = floor.third;
            bttnUp1.Enabled = false;
            bttnUp1to3.Enabled = false;
            bttnDown3.Enabled = true;
            bttnDown3to1.Enabled = true;
            pauseThread = false;            
            motor = new Thread(new ThreadStart(runThread));
            motor.Start();
        
        }
        public void runThread()
        {
            driveLift dlft = new driveLift(replaceTimer);
            pictureBox1.Invoke(dlft);
        }
      
        public void replaceTimer()
       // private void timer1_Tick(object sender, EventArgs e)
        {
            if (pauseThread == true)
            {
               motor.Abort();
               
            }
            else
            {

                doubleBufferPanel2.Refresh();
            switch (which)
            {
                case direction.Up:
                    pictureBox1.Top = pictureBox1.Top - 1;
                    myRatioUp = myRatioUp + 1;
                    if (myRatioUp > 2)
                    {
                        myRatioUp = 0;
                        myAngle = myAngle + 1;
                        rotateNeedle();
                    }
                   
                    break;
                case direction.Down:
                    pictureBox1.Top = pictureBox1.Top + 1;
                    myRatioDown = myRatioDown + 1;
                    if (myRatioDown > 2)
                    {
                        myRatioDown = 0;
                        myAngle = myAngle - 1;
                        rotateNeedle();
                    }
                    break;               
            }
            floorStop();
            Thread.Sleep(liftSpeed);// pause 
            replaceTimer();// repeat
            }
        }


        public void floorStop()
        {
            SoundPlayer simpleSound = new SoundPlayer(Elevator.Properties.Resources.doorbell);
            switch (goingTo)
            {
                case floor.first:
                    if (pictureBox1.Top > 360)
                    {
                        pauseThread = true;                        
                        pictureBox1.Top = 360;                        
                        simpleSound.Play();

                    }
                    break;                   
                case floor.second:
                    if (pictureBox1.Top > 175 && pictureBox1.Top < 185)
                    {
                        pauseThread = true;                       
                        pictureBox1.Top = 180;
                        simpleSound.Play();
                    }
                    break;                  
                case floor.third:
                    if (pictureBox1.Top == 0)
                    {
                        pauseThread = true;                        
                pictureBox1.Top=0;
                simpleSound.Play();
                    }
                    break;
            }         
            
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {           
            liftSpeed = (100 - (int)trackBar1.Value);
           
            label4.Text = ("Lift speed interval = " + liftSpeed.ToString()+ "ms");
        }

        private void doubleBufferPanel1_Paint(object sender, PaintEventArgs e)
        {
            drawFace(e.Graphics);
        }


        public void drawFace(Graphics g)
        {
            g.FillEllipse(Brushes.Black, 5, 5, 90, 90);
            g.FillEllipse(Brushes.Gainsboro, 10, 10, 80, 80);
            g.FillEllipse(Brushes.Black, 30, 30, 40, 40);
            g.DrawString("2", myFont, Brushes.Black, 40, 9);

            g.RotateTransform(-70);
            g.DrawString("1", myFont, Brushes.Black, -39, 22);
            g.RotateTransform(140);
            g.DrawString("3", myFont, Brushes.Black, 55, -70);
            g.ResetTransform();
            g.FillEllipse(Brushes.Red, 40, 40, 20, 20);
        }
       
        public void rotateNeedle()
        {
            Graphics g = doubleBufferPanel1.CreateGraphics();           
            doubleBufferPanel1.Refresh();
            Matrix mX = new Matrix();
            mX.RotateAt((float)myAngle, new PointF(50, 50));       
            g.Transform = mX;
            g.DrawImage(needle, 48, 10);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = doubleBufferPanel1.CreateGraphics();
            drawFace(g);
            rotateNeedle();
        }        
   

        private void bttnExit_Click(object sender, EventArgs e)
        {
            motor.Abort();          
            this.Close();
        }  
       


    }
}
